package com.shujia.java.day20.demo6;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServerSocket {
    public static void main(String[] args) throws Exception {
        //创建ServerSocket
        ServerSocket serverSocket = new ServerSocket(9000);
        //接收接收 获取Socket
        Socket socket = serverSocket.accept();
        //获取输入流
        InputStream is = socket.getInputStream();
        byte[] bytes = new byte[1024];
        //因为客户端写两次 服务器读两次
        //1
        int x = is.read(bytes, 0, bytes.length);
        //10
        int i = Integer.parseInt(new String(bytes, 0, x));
        int y = is.read(bytes, 0, bytes.length);
        //类型转换

        int ii = Integer.parseInt(new String(bytes, 0, y));
        System.out.println(i+"----------"+ii);
        //相加得到运算结果
        int z = i + ii;
        //开始返回数据
        //通过Socket对象调用getOutputStream获取流
        OutputStream os = socket.getOutputStream();
        //类型转换
        bytes=Integer.toString(z).getBytes();
        os.write(bytes,0,bytes.length);
        System.out.println("返回的结果为:"+new String(bytes,0,bytes.length));


    }
}
